/**********************************************************************
 Name:  CalculateHospitalRange
 Purpose: 在PDA上传新的医院拜访记录时，计算当前拜访坐标与对应医院的标准坐标的偏差距离                                                           
-------                                                             
======================================================
History                                                            
-------                                                            
VERSION  AUTHOR               DATE                 DETAIL                                
   1.0 -      huang xiaojiang      03/29/2011      Create          : 
   2.0-       huang xiaojiang      04/19/2011      modified：修改为before update trigger
***********************************************************************/
trigger CalculateHospitalRange on CallPlan__c (before update) {

   //variables
   set<id> setHPId=new set<id>();
   list<CallPlan__c> lCP=new list<CallPlan__c>();
   
    for(CallPlan__c cp:trigger.new){
        if(cp.lat__c!=trigger.oldMap.get(cp.id).lat__c&&cp.lat__c!=null
            &&cp.lon__c!=trigger.oldMap.get(cp.id).lon__c&&cp.lon__c!=null){
            
             setHPId.add(cp.HospitalId__c);  
             lCP.add(cp); 
        }
    }
 
    
   if(setHPId.size()>0){
        map<id,Hospital__c> mapHP=new  map<id,Hospital__c>((list<Hospital__c>)
            [select id,name,lat__c,lon__c from Hospital__c where id in:setHPId]);
     
         //计算门店拜访与标准店面之间偏差
            integer R=6371000;//地球半径，单位米
            double PI=3.1415926;//圆周率
            Hospital__c hpObj=new Hospital__c();
            
            for(CallPlan__c i:lCP){
              
                if(i.HospitalId__c!=null){
                
                    string strHPId=i.HospitalId__c;
                    hpObj=mapHP.get(strHPId);            
                    if(i.lat__c!=null&&i.lon__c!=null&&hpObj!=null){
                            if(hpObj.lat__c!=null&&hpObj.lon__c!=null){
                            double Lat1=double.valueOf(i.lat__c);
                            double Lat2=double.valueOf(mapHP.get(strHPId).lat__c);
                            double Lon1=double.valueOf(i.lon__c);
                            double Lon2=double.valueOf(mapHP.get(strHPId).lon__c);
                            
                            double dLat=(lat2-lat1)*PI/180;
                            double dLon=(lon2-lon1)*PI/180;
                          
                            double a=Math.sin(dLat/2) * Math.sin(dLat/2) +
                                Math.cos(lat1 * PI / 180 ) * Math.cos(lat2 * PI / 180 ) * 
                                Math.sin(dLon/2) * Math.sin(dLon/2); 
                                
                            double c=2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
                            double dRange=R*c;
                            
                            //把距离赋值
                            i.DevRange__c=dRange;                        
                        }else{
                              //有拜访坐标，医院没坐标
                              i.DevRange__c=0;    
                        }
                    }
                            
                }
        
               
            }       
   } 
   
    
    
}